// pages/operation/lesson/lessonIndex/index.js
import { LessonModel } from "../../../../api/api-work/lesson.js"
import { throttle } from "../../../../utils/index.js"
const lessonModel = new LessonModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    pageNum: 1,
    total: 0,
    tableData: [],
    loadStatus: "",
    lessontriggered: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {

  },
  
  searchLesson: throttle(function (e) {
    this.setData({
      pageNum: 1,
      tableData: []
    })
    this.getLessonData()
  }, 500),

  // 获取课程列表
  async getLessonData() {
    lessonModel.getLessonApi({ name: this.data.name, pageNum: this.data.pageNum, pageSize: 10 }).then(res => {
      let rows = this.data.tableData;
      rows.push(...res.rows)
      this.setData({
        tableData: rows,
        total: res.total
      })
      if (this.data.total == 0) {
        this.setData({
          // 无数据
          loadStatus: ""
        })
      } else if (this.data.tableData.length < this.data.total) {
        this.setData({
          loadStatus: "loadmore"
        })
      } else {
        this.setData({
          loadStatus: "nomore"
        })
      }
    })
  },

  getMoreLesson() {
    if (this.data.loadStatus === "nomore") {
      return
    }
    this.setData({
      pageNum: this.data.pageNum + 1,
      loadStatus: "loading"
    })
    this.getLessonData()
  },

  refreshMoreLesson() {
    this.setData({
      lessontriggered: false,
      pageNum: 1,
      tableData: []
    })
    this.getLessonData()
  },

  addLesson() {
    wx.navigateTo({
      url: '/pages/operation/lesson/lessonAdd/index',
    })
  },

  // 课程详情
  clickLesson(e) {
    const query = e.currentTarget.dataset.query;
    var item = JSON.stringify(query);
    wx.navigateTo({
      url: `/pages/operation/lesson/lessonDetail/index?item=${item}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    this.setData({
      tableData: []
    })
    await this.getLessonData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})